package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.thief.ThiefInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ThiefInfoDaoImpl extends TempDao {

    public Map<Integer, ThiefInfo> getThiefInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_thief";
        Map<Integer, ThiefInfo> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ThiefInfo data = new ThiefInfo();
                data.setType(rs.getInt("id"));
                data.setUnlockBuildingId(rs.getInt("unlockBuildingId"));
                data.setWorkBuildingId(rs.getInt("workBuildingId"));
                data.setWorkTime(rs.getInt("workTime"));
                data.setReward(rs.getString("reward"));
                data.setWeight(rs.getInt("weight"));
                data.setPlaceIdList(StringUtils.stringToIntegerList(rs.getString("placeIdList"), ";"));
                dataMap.put(data.getType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

}
